package fr.xgouchet.wedding.shared.model;

import java.util.Date;

import fr.xgouchet.wedding.client.service.WebService;

/**
 * Represents a task in the Agenda
 * 
 * @author x.gouchet
 * 
 */
public class Task {

	/**
	 * Default Constructor
	 */
	public Task() {
		mID = "";
		mTimeBeforeWedding = 0;
		mTitle = "";
		mDescription = "";
		mCategory = "";
		mStatus = false;
	}

	/**
	 * @param id
	 *            the iD to set
	 */
	public void setID(String id) {
		mID = id;
	}

	/**
	 * @param days
	 *            the days before the wedding deadline
	 */
	public void setDaysBeforeWedding(int days) {
		mTimeBeforeWedding = days;
		updateDeadline();
	}

	/**
	 * @param months
	 *            the timeBeforeWedding to set
	 */
	public void setMonthsBeforeWedding(int months) {
		mTimeBeforeWedding = months * 30;
		updateDeadline();
	}

	/**
	 * @param deadline
	 *            the deadline to set
	 */
	public void setDeadline(Date deadline) {
		mDeadline = deadline;
		updateTimeBeforeWedding();
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		mTitle = title;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		mDescription = description;
	}

	/**
	 * @param category
	 *            the category to set
	 */
	public void setCategory(String category) {
		mCategory = category;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(boolean status) {
		mStatus = status;
	}

	/**
	 * @return the id
	 */
	public String getID() {
		return mID;
	}

	/**
	 * @return the timeBeforeWedding
	 */
	public int getDaysBeforeWedding() {
		return mTimeBeforeWedding;
	}

	/**
	 * @return the deadline
	 */
	public Date getDeadline() {
		return mDeadline;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return mTitle;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return mDescription;
	}

	/**
	 * @return the category
	 */
	public String getCategory() {
		return mCategory;
	}

	/**
	 * @return the status
	 */
	public boolean isComplete() {
		return mStatus;
	}

	/**
	 * updates the deadline from the time before wedding
	 */
	private void updateDeadline() {
		long wedding = WebService.getSettings().getWeddingDate().getTime();
		mDeadline = new Date(wedding
				- (mTimeBeforeWedding * DAY_TO_MILLISECONDS));
	}

	/**
	 * updates the time before wedding from the deadline
	 */
	private void updateTimeBeforeWedding() {
		long wedding = WebService.getSettings().getWeddingDate().getTime();
		long deadline = mDeadline.getTime();
		mTimeBeforeWedding = (int) ((wedding - deadline) / DAY_TO_MILLISECONDS);
	}

	/** the number of milliseconds in one day {@value} */
	public static final long DAY_TO_MILLISECONDS = 24 * 60 * 60 * 1000;

	/** the id of the task */
	private String mID;

	/** The latest time before the wedding when the task must be done (in days) */
	private int mTimeBeforeWedding;

	/** The task's deadline */
	private Date mDeadline;

	/** The title of the Task */
	private String mTitle;

	/** The description of the Task */
	private String mDescription;

	/** The category of the task */
	private String mCategory;

	/** is the task complete */
	private boolean mStatus;

}
